﻿/*var data = [{
	"commentId": 11,
	"article": null,
	"user": {
		"userId": 11,
		"nickname": "清风",
		"headPortrait": "http://qzapp.qlogo.cn/qzapp/101477629/B4901BFB60F8DEE83F01692F2544E612/100",
		"sex": "男",
		"registrationDate": "2018-08-01 15:19:26",
		"latelyLoginTime": "2018-08-01 15:19:26",
		"commentNum": 1
	},
	"content": "<p>网站开源吗</p><p><br></p>",
	"commentDate": "2018-08-01 15:19:41",
	"site": "河北省保定市  铁通",
	"reply":[{
			"replyId": 2,
			"comment": {
				"commentId": 11,
				"article": null,
				"user": {
					"userId": 11,
					"nickname": "清风",
					"headPortrait": "http://qzapp.qlogo.cn/qzapp/101477629/B4901BFB60F8DEE83F01692F2544E612/100",
					"sex": "男",
					"registrationDate": "2018-08-01 15:19:26",
					"latelyLoginTime": "2018-08-01 15:19:26",
					"commentNum": 1
				},
				"content": "<p>网站开源吗</p><p><br></p>",
				"commentDate": "2018-08-01 15:19:41",
				"site": "河北省保定市  铁通"
			},
			"user": {
				"userId": 1,
				"nickname": "Single",
				"headPortrait": "http://qzapp.qlogo.cn/qzapp/101477629/2F1EDDE252859E5FF645F959893C6863/100",
				"sex": "男",
				"registrationDate": "2018-07-26 21:24:49",
				"latelyLoginTime": "2018-08-09 10:25:36",
				"commentNum": 1
			},
			"content": "最近有点忙，后期会开源到GitHub上的。",
			"replyDate": "2018-08-01 22:10:28",
			"site": "湖南省湘潭市  移动"
		}]
},  {
	"commentId": 2,
	"article": null,
	"user": {
		"userId": 2,
		"nickname": "Mr.Long",
		"headPortrait": "http://qzapp.qlogo.cn/qzapp/101477629/B5D5212D0429E4491D932EEEF814FE99/100",
		"sex": "男",
		"registrationDate": "2018-07-26 21:30:24",
		"latelyLoginTime": "2018-08-01 22:16:42",
		"commentNum": 4
	},
	"content": "试试<br>",
	"commentDate": "2018-07-26 21:30:52",
	"site": "湖南省湘潭市  移动",
	"reply":[]
}];*/


layui.config({
    base: '.././js/' //你的扩展模块所在目录
}).use(['jquery','layer','ajaxRequest','flow'], function(){
    var flow = layui.flow;
    var layer = layui.layer;
    var ajaxRequest = layui.ajaxRequest;
    var $ = layui.jquery;

    $(function () {
        // 加载文章分类
        getCategory();
        // 加载相似文章
        getSimilarArticleList();
        // 加载随便看看
        getCasualArticleList();
        // 观看数量加一
        addArticleWatch();
        // 获取观看数量
        var theRequest = getRequest();
        var watchNumber = theRequest['watchNumber'];
        $("#watchNumber").html("浏览量："+watchNumber);
    });

    //评论显示
    flow.load({
        elem: '#commentList' //流加载容器
        ,done: function(page, next){ //执行下一页的回调
            setTimeout(function(){
                    var lis = [];

                var param = new Object();

                param.page = page;
                param.limit = commentLimit;
                param.articleId = $("#articleId").val();

                var total = 0;
                $.ajax({
                    type: "POST",
                    url: baseUrl+"/blog/article/getComment",
                    data: param,
                    success: function(res) {
                        if (res.status == 200) {
                            var data = res.data.rows;
                            total = res.data.total;
                            for(var i = 0; i < data.length; i++) {
                                var str = "";
                                for(var r = 0; r < data[i].reply.length; r++) {
                                    str += "<div class=\"comment-child\">\n" +
                                        "      <img src=\"" + data[i].reply[r].user.headPortrait + "\" alt=\"" + data[i].reply[r].user.nickname + "\" />\n" +
                                        "      <div class=\"info\">\n" +
                                        "          <span class=\"username\">	" + data[i].reply[r].user.nickname + " : </span>";
                                    if(data[i].reply[r].user.userId == '1') {
                                        str += "<span class=\"is_bloger\">博主</span>&nbsp;";
                                    }
                                    str += "回复 <span class=\"username\">@" + data[i].user.nickname + "：</span>";
                                    if(data[i].user.userId == '1') {
                                        str += "<span class=\"is_bloger\">博主</span>&nbsp;";
                                    }
                                    str += "<span>" + data[i].reply[r].content + "</span>\n" +
                                        "      </div>\n" +
                                        "      <p class=\"info\"><span class=\"time\">" + data[i].reply[r].commentDate + "</span>&nbsp;&nbsp;&nbsp;&nbsp;<span>" + data[i].reply[r].site + "</span></p>\n" +
                                        "  </div>\n";
                                }
                                lis.push("<li>\n" +
                                    "               <div class=\"comment-parent\">\n" +
                                    "                   <img src=\"" + data[i].user.headPortrait + "\" alt=\"" + data[i].user.nickname + "\" />\n" +
                                    "                   <div class=\"info\">\n" +
                                    "                       <span class=\"username\">" + data[i].user.nickname + "</span>\n");
                                if(data[i].user.userId == '1') {
                                    lis.push("<span class=\"is_bloger\">博主</span>&nbsp;");
                                }
                                lis.push("                   </div>\n" +
                                    "                   <div class=\"content\">\n" +
                                    "                       " + data[i].content + "\n" +
                                    "                   </div>\n" +
                                    "                   <p class=\"info info-footer\"><span class=\"time\">" + data[i].commentDate + "</span>&nbsp;&nbsp;&nbsp;&nbsp;<span>" + data[i].site + "</span>&nbsp;&nbsp;<a class=\"btn-reply\" style=\"color: #009688;font-size:14px;\" href=\"javascript:;\" onclick=\"btnReplyClick(this)\">回复</a></p>\n" +
                                    "               </div>\n" +
                                    "               <hr />\n" + str +
                                    "               <!-- 回复表单默认隐藏 -->\n" +
                                    "               <div class=\"replycontainer layui-hide\">\n" +
                                    "                   <form class=\"layui-form\" action=\"/reply/list/\">\n" +
                                    "                   <input type=\"hidden\" id=\"comment\" name=\"comment\" value=\"" + data[i].commentId + "\" />\n" +
                                    "                   <input type=\"hidden\" id=\"user\" lay-verify=\"userId\" name=\"user\" value=\"" + $('#user').val() + "\" />\n" +
                                    "                       <div class=\"layui-form-item\">\n" +
                                    "                           <textarea name=\"content\" lay-verify=\"replyContent\" placeholder=\"回复  @" + data[i].user.nickname + ":\" class=\"layui-textarea\" style=\"min-height:80px;\"></textarea>\n" +
                                    "                       </div>\n" +
                                    "                       <div class=\"layui-form-item\">\n" +
                                    "                           <button class=\"layui-btn layui-btn-mini\" lay-submit=\"formReply\" lay-filter=\"formReply\">提交</button>\n" +
                                    "                       </div>\n" +
                                    "                   </form>\n" +
                                    "               </div>\n" +
                                    "           </li>");
                            }
                        }

                        //执行下一页渲染，第二参数为：满足“加载更多”的条件，即后面仍有分页
                        //pages为Ajax返回的总页数，只有当前页小于总页数的情况下，才会继续出现加载更多
                        next(lis.join(''), page < Math.ceil(total/commentLimit));
                    },
                    error: function(data) {
                        layer.msg("网络错误！");
                    }
                });


            }, 500);
        }
    });

    // 加载文章分类
    function getCategory() {
        var res = ajaxRequest.sendGet("/blog/article/getCategory",{},false,false);
        var html = "<a href=\"javascript:classifyList(''); \">全部</a>";
        if (res) {
            $.each(res.data,function(index,element){
                html = html + '<a href="javascript:classifyList('+element.id+'); ">'+element.name+'</a>';
            });
            $(".article-category-title").after(html);
        }
    }

    // 加载相似文章
    function getSimilarArticleList() {
        var categoryId = $("#categoryId").val();
        var res = ajaxRequest.sendGet("/blog/article/getSimilarArticleList",{'categoryIds':categoryId,'limit':similarAndCasual},false,false);
        var html = "";
        if (res) {
            $.each(res.data,function(index,element){
                var classa = "";
                if (index == 0){
                    classa = "layui-bg-red";
                } else if (index == 1) {
                    classa = "layui-bg-orange";
                }else if (index == 2) {
                    classa = "layui-bg-green";
                }

                html = html + "<li>\n" +
                    "<span>\n" +
                    "<i class=\"layui-badge-rim "+classa+" \">"+Number(index+1)+"</i>\n" +
                    "</span>\n" +
                    "&nbsp;&nbsp;<a href=\""+articleUrl+element.id+".html?watchNumber="+element.watchNumber+" \">"+element.title+"</a>\n" +
                    "</li>";
            });
            $(".similar").html(html);
        }
    }

    // 加载随便看看
    function getCasualArticleList() {
        var res = ajaxRequest.sendGet("/blog/article/getCasualArticleList",{'limit':similarAndCasual},false,false);
        var html = "";
        if (res) {
            $.each(res.data,function(index,element){
                var classa = "";
                if (index == 0){
                    classa = "layui-bg-red";
                } else if (index == 1) {
                    classa = "layui-bg-orange";
                }else if (index == 2) {
                    classa = "layui-bg-green";
                }

                html = html + "<li>\n" +
                    "<span>\n" +
                    "<i class=\"layui-badge-rim "+classa+" \">"+Number(index+1)+"</i>\n" +
                    "</span>\n" +
                    "&nbsp;&nbsp;<a href=\""+articleUrl+element.id+".html?watchNumber="+element.watchNumber+" \">"+element.title+"</a>\n" +
                    "</li>";
            });
            $(".casual").html(html);
        }
    }

    // 观看数量加一
    function addArticleWatch() {
        var articleId = $("#articleId").val();
        var res = ajaxRequest.sendGet("/blog/article/addArticleWatch",{'articleId':articleId},false,false);
    }
});

$(document).ready(function() {
    $(".fa-file-text").parent().parent().addClass("layui-this");
});

function classifyList(id) {
	window.location.href = indexUrl + "article.html?category="+id;
}

// 其它页面跳转过来时获取传入参数
function getRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}